package tech.aistar.corejava.day12.dayhomeork;

import java.util.*;

/**
 * Created by 豆梦寒 on 2019/7/31.
 */
public class ProductTotal {
    public static void main(String[] args) {
        List<Products> list = new ArrayList<>();
        Products p1 = new Products("宝洁","洗手粉",18.5);
        Products p2 = new Products("联合利华","肥皂",4.5);
        Products p3 = new Products("宝洁","牙膏",32.5);
        Products p4 = new Products("宝洁","毛巾",14.5);
        Products p5 = new Products("洁利","洗面奶",26.0);
        Products p6 = new Products("好迪","洗发水",27.5);
        Products p7 = new Products("多芬","沐浴露",38.5);
        Products p8 = new Products("宝洁","洗洁精",3.4);

        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);
        list.add(p5);
        list.add(p6);
        list.add(p7);
        list.add(p8);

        System.out.println(list);
        System.out.println("=====================");


        HashMap<String ,Double> m=new HashMap<>();


        Iterator<Products> iterator=list.iterator();
        while (iterator.hasNext()){
            Products p=iterator.next();
            if(m.get(p.getBrand())==null){
                m.put(p.getBrand(),p.getCost());
            }else {
                Double s=m.get(p.getBrand());
                s+=p.getCost();
                m.put(p.getBrand(),s);
            }
        }


        List<Products> list1=new ArrayList<>();


        Set<String > keyset=m.keySet();
        Iterator<String> iter=keyset.iterator();
        while(iter.hasNext()){
            String it=iter.next();
            System.out.println(it+":"+m.get(it));
            list1.add(new Products(it,m.get(it)));
        }

    }
}
